package com.ccx.coachsite.api;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.ccx.coachsite.weibo.auth2.net.Weibo;
import com.ccx.coachsite.weibo.auth2.vo.LoginUser;


public class LocalAccessor{

    private static final String TAG = "LocalAccessor";
	private static final String DATABASE_NAME = "weibodata.db";	

	private Context ctx;	

	private LocalAccessor(Context ctx){
		this.ctx = ctx;
		SQLiteDatabase db = openDB();
		// ************************************初始化数据开始***********************************//
		//创建微博登录帐号表
		db.execSQL(Weibo.LoginTable.CREATE_TABLE);
		// ************************************初始化数据结束***********************************//
		
		db.close();
	}	

	static private LocalAccessor accessor; 
	
	public static LocalAccessor getInstance(Context context){

		if(accessor == null){
			accessor = new LocalAccessor(context);
		}
		return accessor;
	}
	
	private SQLiteDatabase openDB(){		
		return ctx.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
	}
    
    //微博数据库操作
    /**
     * 添加登陆用户信息到数据库表
     * @param sql
     * @param obj
     * @return
     */
    public void addLoginUser(String sql, Object[] obj) {
    	SQLiteDatabase db = openDB();
    	try {
        	db.execSQL(sql, obj);
		} catch (SQLException s) {
			s.printStackTrace();
		}finally{
			db.close();
		}
    }
    
    public void deleteAllLoginUser(String sql) {
    	SQLiteDatabase db = openDB();
    	try {
        	db.execSQL(sql);
		} catch (SQLException s) {
			s.printStackTrace();
		}finally{
			db.close();
		}
    }
    
    public LoginUser getLoginUser(String sql) {
    	
    	SQLiteDatabase db = openDB();
    	Cursor cs=null;
		try {
			cs = db.rawQuery(sql, null);
			
			if(cs!=null && cs.getCount()>0)
				return cusorToLoginUser(cs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(cs!=null){
				cs.close();
				db.close();
			}
		}
		
		return null;
    }
    
	public static LoginUser cusorToLoginUser(Cursor cs)
	{
		if(cs!=null)
		{
			int idIndex=cs.getColumnIndex(Weibo.LoginTable.ID);
			int userIdIndex=cs.getColumnIndex(Weibo.LoginTable.USER_ID);
			int screenNameIndex=cs.getColumnIndex(Weibo.LoginTable.SCREEN_NAME);
			int accessTokenIndex=cs.getColumnIndex(Weibo.LoginTable.ACCESS_TOKEN);
			int accountIndex=cs.getColumnIndex(Weibo.LoginTable.ACCOUNT);
			int packageNameIndex=cs.getColumnIndex(Weibo.LoginTable.PACKAGE_NAME);
			int loginTimeIndex=cs.getColumnIndex(Weibo.LoginTable.LOGIN_TIME);
			if(cs.moveToNext())
			{
				LoginUser loginUser=new LoginUser();
				loginUser.setId(cs.getLong(idIndex));
				loginUser.setUid(cs.getString(userIdIndex));
				loginUser.setScreenName(cs.getString(screenNameIndex));
				loginUser.setAccessToke(cs.getString(accessTokenIndex));
				loginUser.setAccount(cs.getString(accountIndex));
				loginUser.setPackageName(cs.getString(packageNameIndex));
				loginUser.setLoginTime(cs.getLong(loginTimeIndex));
				return loginUser;
			}
			
		}//end if
		
		return null;
		
	}//end LoginUser

}
